package com.poobo.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.poobo.base.impl.BaseDaoImpl;
import com.poobo.dao.IConfigListDao;
import com.poobo.entity.TbConfigList;
import com.poobo.entity.TbResources;
import com.poobo.util.pager.Page;
import com.poobo.util.str.StrUtil;

@Repository
public class ConfigListDaoImpl extends BaseDaoImpl implements IConfigListDao {

	@Override
	public TbConfigList queryConfigListById(Integer id) {
		return selectObjectById(TbConfigList.class, id);
	}

	@Override
	public Page queryConfigListPage(TbConfigList configList, Page page) {
		String hql = " from TbConfigList where 1=1 ";
		String countHql = " select count(*) from TbConfigList where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		String orderBy = " order by isEnable desc,isGenerationTxt desc,id desc ";
		StringBuffer where = new StringBuffer();
		if( configList != null ){
			if( !StrUtil.isEmpty(configList.getName()) && configList.getName() != "" ){
				where.append(" and name like ? ");
				params.add("%" + configList.getName() + "%");
			}
			if( configList.getIsEnable() != null ){
				where.append(" and isEnable = ? ");
				params.add(configList.getIsEnable());
			}
			if( configList.getIsGenerationTxt() != null ){
				where.append(" and isGenerationTxt = ? ");
				params.add(configList.getIsGenerationTxt());
			}
		}
		page.setBaseHql(hql + where.toString() + orderBy);
		page.setCountHql(countHql + where.toString());
		page.setParam(params);
		return getPageByHql(page);
	}

	@Override
	public void deleteConfigListById(Integer id) {
		String hql = " delete from TbConfigList where id = "+ id +" ";
		delObjectByIds(hql);
	}

	@Override
	public void saveConfigList(TbConfigList configList) {
		saveOrUpdate(configList);
	}

	@Override
	public List<TbConfigList> queryAll() {
		String hql = " from TbConfigList ";
		return (List<TbConfigList>) findAll(hql);
	}

	@Override
	public List<TbConfigList> queryByIsEnable() {
		String hql = " from TbConfigList where isEnable=1 ";
		return (List<TbConfigList>) findAll(hql);
	}

	@Override
	public TbConfigList queryConfigListByName(String name) {
		String hql = " from TbConfigList where name='"+name+"'";
		List<TbConfigList> configLists = (List<TbConfigList>) findAll(hql);
		if (configLists!=null && configLists.size()>0) {
			return configLists.get(0);
		}
		return null;
	}

}
